#include "studentwidget.h"
#include "ui_studentwidget.h"
#include <QStringList>
#include <QDebug>
#include <QMessageBox>

StudentWidget::StudentWidget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::StudentWidget)
{
    ui->setupUi(this);
    this->setWindowTitle("学生端");
    this->model=new QStandardItemModel;
    this->setM=new modelSet;
    this->ui->tableView->setModel(model);
    this->ui->btn_confirm->hide();
    this->ui->btn_quit->hide();
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
    connect(ui->tableView->horizontalHeader(),SIGNAL(sectionDoubleClicked(int)),this, SLOT(sortView(int)));
    ui->tableView->setSelectionMode(QAbstractItemView::SingleSelection);
    ui->tableView->setSelectionBehavior(QAbstractItemView::SelectRows);
}

StudentWidget::~StudentWidget()
{
    delete ui;
}

void StudentWidget::getLoginId(QString loginId)
{
    this->loginId=loginId;
}

void StudentWidget::setStuObject()
{
    QStringList studentLine;
    QStringList selectedCourse;
    this->file1->readFile("student.csv",studentLine);
    for(int i=0;i<studentLine.length();i++)
    {
        QString line=studentLine.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");

        if(loginId==linesplit.at(1))
        {
            //先读取后面的课程（有多个）
            for(int j=7;j<linesplit.length();j++)
            {
                selectedCourse.append(linesplit.at(j));
            }
            this->stuInfo1=new student;
            this->stuInfo1->setInfo(linesplit.at(0),linesplit.at(1),linesplit.at(3),linesplit.at(4),linesplit.at(5),linesplit.at(6),linesplit.at(2),selectedCourse);
            this->stuInfo1->loadAboutInfo();
        }
    }
    this->ui->stuName->setText("欢迎"+this->stuInfo1->getName()+"同学登陆系统！");
}

void StudentWidget::setStuScore()
{
    QStringList _studentLine;
    QStringList score;
    this->file1->readFile("score.csv",_studentLine);
    for(int i=0;i<_studentLine.length();i++)
    {
        QString line=_studentLine.at(i);
        line=line.trimmed();
        QStringList linesplit=line.split(",");

        if(loginId==linesplit.at(1))
        {
            //先读取后面的成绩
            for(int j=2;j<linesplit.length();j++)
            {
                score.append(linesplit.at(j));
            }
            this->stuInfo2=new student;
            this->stuInfo2->setScore(linesplit.at(0),linesplit.at(1),score);
        }
    }
}

void StudentWidget::on_btn_exit_clicked()
{
    this->stuInfo1->saveAllInfo();
    this->close();
}

void StudentWidget::on_btn_check_myinfo_clicked()
{
    this->model->clear();
    this->stuInfo1->checkMyInfo(this->model,0);
}

void StudentWidget::on_btn_check_score_clicked()
{
    this->model->clear();
    this->stuInfo2->checkMyScore(this->model,0);
}

void StudentWidget::on_btn_display_all_clicked()
{
    this->stuInfo1->checkAllCourse(this->model);
}

void StudentWidget::on_btn_display_course_clicked()
{
    this->model->clear();
    this->stuInfo1->checkMyCourse(this->model);
}

void StudentWidget::on_btn_giveup_course_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(model->item(row,1)->text().length()==5)
        {
            QModelIndex index=this->model->index(row,0);
            QString selectId=model->data(index).toString();
            if(this->stuInfo1->giveUpCourse(selectId))
            {
                QMessageBox::information(this,"注意","退课成功！","确认");
            }
            else
            {
                QMessageBox::critical(this,"错误","退课失败，请检查！","确认");
            }
        }
        else
        {
            QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
    }
    this->ui->btn_display_course->click();
}

void StudentWidget::on_btn_modify_password_clicked()
{
    this->ui->btn_confirm->show();
    this->ui->btn_quit->show();
    this->ui->oldPassword->show();
    this->ui->newPassword->show();
}

void StudentWidget::on_btn_confirm_clicked()
{
    QString oldPassword=this->ui->oldPassword->text();
    QString newPassword=this->ui->newPassword->text();
    if(this->stuInfo1->modifyPassword(oldPassword,newPassword))
    {
        if(QMessageBox::information(this,"修改成功！","密码修改成功","确认")==0)
        {
            this->ui->btn_confirm->hide();
            this->ui->btn_quit->hide();
            this->ui->oldPassword->hide();
            this->ui->newPassword->hide();
        }
    }
    else
    {
        QMessageBox::critical(this,"修改失败！","旧密码输入错误或新密码不足六位！","确认");
    }
}

void StudentWidget::on_btn_select_course_clicked()
{
    int row=this->ui->tableView->currentIndex().row();
    if(row!=-1)
    {
        if(model->item(row,1)->text().length()==5)
        {
            QString selectId=model->data(this->model->index(row,0)).toString();
            QString name=model->data(this->model->index(row,2)).toString();
            QString time=model->data(this->model->index(row,9)).toString();
            if(this->stuInfo1->selectCourse(selectId,name,time))
            {
                QMessageBox::information(this,"注意","选课成功！","确认");
                this->on_btn_display_course_clicked();
            }
            else
            {
                QMessageBox::critical(this,"注意","选课冲突！","确认");
            }
        }
        else
        {
            QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
        }
    }
    else
    {
        QMessageBox::critical(this,"错误","请先选择相应课程！","确认");
    }
}

void StudentWidget::on_btn_quit_clicked()
{
    this->ui->btn_confirm->hide();
    this->ui->btn_quit->hide();
    this->ui->oldPassword->hide();
    this->ui->newPassword->hide();
}

void StudentWidget::sortView(int a)
{
    if(flag)
        this->setM->quickSort(0, model->rowCount()-1,a,model);
    else
        this->setM->quickSort_1(0, model->rowCount()-1,a,model);
    flag=!flag;
}
